//
//  TF_BackNetWorkSelectView.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/1/19.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
protocol TF_BackNetworkSelectDelegate:class {
    func setNetwork(Index:Int)
    
    /**选择网络服务商*/
//    -(void)setNetwork:(NSString *_Nonnull)operatorID selectedSegmentIndex:(NSInteger )Index callback:(void (^ _Nullable)(NSInteger code, TFError * _Nullable error))callback;
    
}
class TF_BackNetWorkSelectView: UIView,UITableViewDelegate,UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "backnetselectID", for: indexPath) as! TF_BackNetSelectCell
        cell.Cellwidth = self.frame.size.width;
        cell.selectionStyle = .none
        let model = self.dataSource[indexPath.row] as! TF_BackNetWorkModel
        cell.setNetSelectMdoel(model)
        cell.titleLab.text = "线路\(indexPath.row+1)"
    
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        self.dataSource.forEach { netModel in
            if let m = netModel as? TF_BackNetWorkModel {
                m.isSelect = false
            }
        }
       
        
       let model = self.dataSource[indexPath.row] as? TF_BackNetWorkModel
         model?.isSelect = true
        self.networkdelegate?.setNetwork(Index: model!.index)
        self.tableview.reloadData()
       
    }
    /**关闭*/
    var closeBtn:UIButton!
    /**title*/
    var titleLab:UILabel!
    /**tips*/
    var tipsLab:UILabel!
    var tableview:UITableView!
    var dataSource:NSMutableArray = NSMutableArray.init()
    //yes 为显示
    @objc public  var isShow:Bool = false
   
    weak var networkdelegate: TF_BackNetworkSelectDelegate?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.closeBtn = UIButton.init()
        self.closeBtn.setImage(UIImage.init(named: "HTLive.bundle/close_gray"), for: .normal)
        addSubview(self.closeBtn)
       
        
        self.titleLab = UILabel.init()
        self.titleLab.text = "网络选择"
        self.titleLab.font = UIFont.PingFang(size: 19)
        self.titleLab.textAlignment = .center
        self.titleLab.textColor = UIColor(red: 20/255.0, green: 23/255.0, blue: 26/255.0, alpha: 1.0)
        addSubview(self.titleLab)
        
        self.tipsLab = UILabel.init()
        self.tipsLab.text = "95%的情况下，自动选择的线路最好";
        self.tipsLab.font = UIFont.PingFang(size: 15)
        self.tipsLab.textColor = UIColor(red: 65/255.0, green: 150/255.0, blue: 235/255.0, alpha: 1.0)
        
        self.tipsLab.textAlignment = .center
        addSubview(self.tipsLab)
      
        self.tableview = UITableView.init()
 
        self.tableview.backgroundColor = UIColor.clear
        self.tableview.delegate = self
        self.tableview.dataSource = self
        addSubview(self.tableview)
        self.tableview.rowHeight = 55
        self.tableview.separatorStyle = .none

        self.tableview.register(TF_BackNetSelectCell.self, forCellReuseIdentifier: "backnetselectID")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.closeBtn.frame = CGRect(x: self.frame.size.width-40, y: 10, width: 32, height: 32)
        self.titleLab.frame = CGRect(x: 0, y: 20, width: self.frame.size.width, height: 20)
        self.tipsLab.frame = CGRect(x: 0, y: self.titleLab.frame.maxY+10, width: self.frame.size.width, height: 20)
        
        self.tableview.frame = CGRect(x: 0, y: self.tipsLab.frame.maxY+10, width: self.frame.size.width, height: self.frame.size.height-self.tipsLab.frame.maxY-10)
    }
    
   //TFPlaybackLineManager?  TFPlaybackLineData
    func updataNetWork(_ model:TFPlaybackLineManager) {
    
        let arr = model.data
       
        if (arr.count == 0  ) {
              return
          }
        if model.data.count == self.dataSource.count {
            return
        }
        self.dataSource.removeAllObjects()
        for i in 0..<arr.count {
          
            let livemodel = arr[i] as? TFPlaybackLineData
            let netmodel = TF_BackNetWorkModel.init()
            netmodel.current = livemodel!.current
            netmodel.index = livemodel!.index
            netmodel.name = livemodel!.name

            if i==0 {
                netmodel.isSelect = true
            }
            self.dataSource.add(netmodel)
        
        }
        self.tableview.reloadData()
      
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}
